<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    /* function get(url, data = "", async = true, dataType = "text", success) {
        var xhr = new XMLHttpRequest();

        xhr.open("get", data ? url + "?" + data : url, async);

        xhr.send();

        xhr.onreadystatechange = function () {
            // 2 3 4
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;

                if (dataType == "json") {
                    result = JSON.parse(result);
                }

                if (success && typeof success == "function") {
                    success(result);
                }


            }
        }

    }*/



    function get(options) {

        var { url, data = "", async = true, dataType = "text", success } = options;

        if (Object.prototype.toString.call(data) == "[object Object]") {
            try {
                var params = new URLSearchParams(data);
                data = params.toString();
            } catch {
                var list = [];
                for (var key in data) {    // key => "user"
                    var val = data[key];  // "a123123"

                    if (Array.isArray(val)) {
                        for (var i = 0; i < val.length; i++) {
                            var item = val[i];
                            var str = key + "=" + item;  // "user=a123123"
                            list.push(str);
                        }
                    } else {
                        var str = key + "=" + val;  // "user=a123123"
                        list.push(str);
                    }

                }
                // console.log(list);

                data = list.join("&");
            }
        }

        var xhr = new XMLHttpRequest();

        xhr.open("get", data ? url + "?" + data : url, async);

        xhr.send();

        xhr.onreadystatechange = function () {
            // 2 3 4
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;

                if (dataType == "json") {
                    result = JSON.parse(result);
                }

                if (success && typeof success == "function") {
                    success(result);
                }


            }
        }

    }






</script>

</html>